import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import { Message } from 'element-ui' 
import { cookieGet, cookieSet } from '../storageCookie'
import { login } from '../service/service'



const routes = [

  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/manage',
    component: () => import('../views/manage.vue'),
    meta: { name: '首页' },
    children: [
      {
        path: '/home',
        component: () => import('../views/home.vue'),
        name: 'home',
        meta: { name: '首页' },
      },
      {
        path: '/HotTopicDetail',
        component: () => import('../views/HotTopicDetail.vue'),
        name: 'HotTopicDetail',
        meta: { name: '热点时间详情页' },
      },
      {
        path: '/SearchPage',
        component: () => import('../views/SearchPage.vue'),
        name: 'SearchPage',
        meta: { name: '搜索页' },
      },
      {
        path: '/DetailsPage',
        component: () => import('../views/DetailsPage.vue'),
        name: 'DetailsPage',
        meta: { name: '详情页' },
      },
      {
        path: '/BookDetailsPage',
        component: () => import('../views/BookDetailsPage.vue'),
        name: 'BookDetailsPage',
        meta: { name: '图书详情页' },
      },
      {
        path: '/StatisticalAnalysis',
        component: () => import('../views/StatisticalAnalysis.vue'),
        name: 'StatisticalAnalysis',
        meta: { name: '统计分析' },
      },
      {
        path: '/VideoDetails',
        component: () => import('../views/VideoDetails.vue'),
        name: 'VideoDetails',
        meta: { name: '视频详情页' },
      },
      {
        path: '/PersonalCenter',
        component: () => import('../views/PersonalCenter.vue'),
        name: 'PersonalCenter',
        meta: { name: '个人中心' },
        children: [
          {
            path: 'MyCollection',
            component: () => import('../views/MyCollection.vue'),
            name: 'MyCollection',
            meta: { name: '我的收藏' }
          },
          {
            path:'Mybrowsing',
            component: () => import('../views/Mybrowsing.vue'),
            name: 'Mybrowsing',
            meta: { name: '我的浏览' }
          },

          {
            path:'Mysearch',
            component: () => import('../views/Mysearch.vue'),
            name: 'Mysearch',
            meta: { name: '我的搜索' }
          },
          {
            path:'MyDownloads',
            component: () => import('../views/MyDownloads.vue'),
            name: 'MyDownloads',
            meta: { name: '我的下载' }
          },
          {
            path:'MyFeedback',
            component: () => import('../views/MyFeedback.vue'),
            name: 'MyFeedback',
            meta: { name: '我的反馈' }
          },
          {
            path:'RecordOflncorrectWords',
            component: () => import('../views/RecordOflncorrectWords.vue'),
            name: 'RecordOflncorrectWords',
            meta: { name: '错词记录' }
          },
          {
            path:"Myclaim",
            component: () => import('../views/Myclaim.vue'),
            name: 'Myclaim',
            meta: { name: '我的认领' }
          }
        ]
      }

    ]
  },


  // 404路由：重定向到已存在的路径（/manage）
  {
    path: '*',
    redirect: '/' // 假设未匹配的路径跳转到登录页
  },
]



var originalPush = VueRouter.prototype.push;
// 修改 原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(function err() { err })
}

var router = new VueRouter({
  mode: 'history',
  // base: process.env.BASE_URL,
  routes,

});
// 如果想限制所有路由的跳转，可以使用router对象调用全局路由守卫
router.beforeEach(async function (to, from, next) {
  const token = cookieGet('tsgfAuthorization')
  if (token) {
  next()
  } else {
    try {
      const res = await login()
      if (res.code === 200) {
        cookieSet("tsgfAuthorization", res.data.token);
        next() // 登录成功，放行
      } else {
        Message.error(res.data.msg || '登录失败')
        // 登录失败，跳转至错误页或其他处理
      }
    } catch (error) {
      //   Message.error(error || '登录失败')
      //  console.error('自动登录失败', error)
    }
  }
})

export default router
